﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreRay.Samplers;
using CoreRay.Cameras;
using CoreRay.Integrators;

namespace CoreRay
{
    public class Scene
    {
        public Scene()
        {
            this.SceneObjects = new List<SceneObject>();

            this.SceneViews = new Dictionary<string, SceneView>();
            this.AddSceneView(new ScenegraphView());
        }

        public Camera MainCamera
        {
            get;
            set;
        }

        public List<SceneObject> SceneObjects
        {
            get;
            private set;
        }

        public ScenegraphView ScenegraphView
        {
            get
            {
                return (ScenegraphView) this.SceneViews["Scenegraph"];
            }
        }

        public Dictionary<string, SceneView> SceneViews
        {
            get;
            private set;
        }

        public void AddSceneView(SceneView sceneView)
        {
            this.SceneViews.Add(sceneView.Name, sceneView);
        }

        public void AddSceneObject(SceneObject sceneObject)
        {
            AddSceneObject(sceneObject, null);
        }

        public void AddSceneObject(SceneObject sceneObject, SceneObject parent)
        {
            this.SceneObjects.Add(sceneObject);

            foreach (var sceneView in this.SceneViews.Values)
            {
                sceneView.OnSceneObjectAdded(parent, sceneObject);
            }
        }

        public Intersection Intersect(Ray ray)
        {
            Intersection result = null;

            foreach (SceneObject sceneObject in this.SceneObjects)
            {
                GeometryObject geometryObject = sceneObject as GeometryObject;

                if (geometryObject != null)
                {
                    if (geometryObject.WorldBoundingBox.Intersect(ray))
                    {
                        Intersection intersection = geometryObject.Intersect(ray);

                        if (intersection != null && (result == null || result.RayT > intersection.RayT))
                        {
                            result = intersection;
                        }
                    }
                }
            }

            return result;
        }

        public bool IntersectShadow(Ray ray)
        {
            foreach (SceneObject sceneObject in this.SceneObjects)
            {
                GeometryObject geometryObject = sceneObject as GeometryObject;

                if (geometryObject != null)
                {
                    if (geometryObject.WorldBoundingBox.Intersect(ray))
                    {
                        if (geometryObject.IntersectPosition(ray))
                        {
                            return true;
                        }
                    }
                }
            }

            return false;
        }
    }
}
